<?php

namespace App\Kuafu\Sys\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Sys\Enum\SysCacheEnum;
use Illuminate\Support\Facades\Cache;

class Menus extends BaseModel
{
    use KfSoftDeletes, KfUid;

    protected $table = 'menus';

    protected $fillable    = ['id', 'name', 'url', 'parentId', 'type', 'status', 'createdByUid', 'updatedByUid'];
    protected $modelCached = true;

    public const STATUS_UP   = 0;
    public const STATUS_DOWN = 1;

    public const TYPE_WEB  = 0; // 运营后台
    public const TYPE_POS  = 1; // POS 点餐
    public const TYPE_SHOP = 2; // 商家app

    /**
     * 获取所有菜单
     *
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public static function getAll($type = self::TYPE_WEB)
    {
        $expTime = 0;
        if (config('app.env') == 'dev') {
            $expTime = 0;
        }
//        Cache::forget(SysCacheEnum::getSysMenuList());
        return Cache::remember(SysCacheEnum::getSysMenuList(), $expTime, function () use ($type) {
            return self::query()
                       ->where('type', $type)
                       ->orderBy('sort')
                       ->orderBy('id')
                       ->get([
                           'id',
                           'alias',
                           'name',
                           'url',
                           'parentId',
                           'type',
                           'isShow',
                           'status',
                           'sort'
                       ]);
        });
    }
}
